home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Game Programming in C++ - Start to Finish
/
GameProgrammingS.iso
/
Peon
/
PeonSDK-Win32-1.0.0.exe
/
{app}
/
PeonMain
/
source
/
Vector3.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-11-18
|
3KB
|
173 lines
#include "Vector3.h"
namespace peon
{
Vector3::Vector3( float x_, float y_, float z_ )
{
x = x_;
y = y_;
z = z_;
}
Vector3::~Vector3()
{
}
void Vector3::set( float x_, float y_, float z_ )
{
x = x_;
y = y_;
z = z_;
}
float Vector3::length( void )
{
return( (float)sqrt( x * x + y * y + z * z ) );
}
void Vector3::normalize( void )
{
float fLength = length();
x = x / fLength;
y = y / fLength;
z = z / fLength;
}
// Static utility methods...
static float distance( const Vector3 &v1, const Vector3 &v2 )
{
float dx = v1.x - v2.x;
float dy = v1.y - v2.y;
float dz = v1.z - v2.z;
return (float)sqrt( dx * dx + dy * dy + dz * dz );
}
static float dotProduct( const Vector3 &v1, const Vector3 &v2 )
{
return( v1.x * v2.x + v1.y * v2.y + v1.z * v2.z );
}
static Vector3 crossProduct( const Vector3 &v1, const Vector3 &v2 )
{
Vector3 vCrossProduct;
vCrossProduct.x = v1.y * v2.z - v1.z * v2.y;
vCrossProduct.y = v1.z * v2.x - v1.x * v2.z;
vCrossProduct.z = v1.x * v2.y - v1.y * v2.x;
return vCrossProduct;
}
// Operators...
Vector3 Vector3::operator + ( const Vector3 &other )
{
Vector3 vResult(0.0f, 0.0f, 0.0f);
vResult.x = x + other.x;
vResult.y = y + other.y;
vResult.z = z + other.z;
return vResult;
}
Vector3 Vector3::operator + ( void ) const
{
return *this;
}
Vector3 Vector3::operator - ( const Vector3 &other )
{
Vector3 vResult(0.0f, 0.0f, 0.0f);
vResult.x = x - other.x;
vResult.y = y - other.y;
vResult.z = z - other.z;
return vResult;
}
Vector3 Vector3::operator - ( void ) const
{
Vector3 vResult(-x, -y, -z);
return vResult;
}
Vector3 Vector3::operator * ( const Vector3 &other )
{
Vector3 vResult(0.0f, 0.0f, 0.0f);
vResult.x = x * other.x;
vResult.y = y * other.y;
vResult.z = z * other.z;
return vResult;
}
Vector3 Vector3::operator * ( const float scalar )
{
Vector3 vResult(0.0f, 0.0f, 0.0f);
vResult.x = x * scalar;
vResult.y = y * scalar;
vResult.z = z * scalar;
return vResult;
}
Vector3 operator * ( const float scalar, const Vector3 &other )
{
Vector3 vResult(0.0f, 0.0f, 0.0f);
vResult.x = other.x * scalar;
vResult.y = other.y * scalar;
vResult.z = other.z * scalar;
return vResult;
}
Vector3 Vector3::operator / ( const Vector3 &other )
{
Vector3 vResult(0.0f, 0.0f, 0.0f);
vResult.x = x / other.x;
vResult.y = y / other.y;
vResult.z = z / other.z;
return vResult;
}
Vector3& Vector3::operator = ( const Vector3 &other )
{
x = other.x;
y = other.y;
z = other.z;
return *this;
}
Vector3& Vector3::operator += ( const Vector3 &other )
{
x += other.x;
y += other.y;
z += other.z;
return *this;
}
Vector3& Vector3::operator -= ( const Vector3 &other )
{
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
}